class RepliesController < ApplicationController
  include AuthenticatedSystemBrowse
  before_filter :login_required, :only => [ :new, :edit, :destroy ]
  
  #cache_sweeper :post_sweeper
  
  def destroy
    @reply = Reply.find(params[:id])
    if @reply.user_has_auth_destroy?(current_user)
    	@reply.destroy
    	redirect_post_url(@reply.post)
    else
    	raise ActiveRecord::RecordNotFound
    end
  end
  
  def new
    @post = Post.find(:first, :conditions => [ "id = ?", params[:post][:id]])
    reply = Reply.new(params[:reply])
    return unless request.post?
    Topic.transaction do
        reply.post = @post
        reply.user = current_user
        reply.save!
        @post.disable_ferret
        @post.update_attributes(:replied_at => Time.now.utc, :replies_count => @post.replies_count + 1)
        current_user.add_scores(1)
    end
    redirect_post_url(@post)
    rescue ActiveRecord::RecordInvalid
    redirect_post_url(@post)
  end
  
  def edit
    @reply = current_user.replies.find(params[:id])
    return unless request.post?
    @reply.update_attributes(params[:reply])
    redirect_post_url(@reply.post)
  end
  
  def show_form
    @post = Post.find(:first, :conditions => [ "id = ?", params[:id]])
    render(:partial => "form")
  end
  
  def cacel_reply
    render(:text => "")
  end
end
